package com.gduf.lsm.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.gduf.lsm.common.constant.ActivityStatusConstant;
import com.gduf.lsm.common.enums.ResultCodeEnum;
import com.gduf.lsm.entity.*;
import com.gduf.lsm.exception.CustomException;
import com.gduf.lsm.mapper.CulturalMapper;
import com.gduf.lsm.mapper.JoinCulturalMapper;
import com.gduf.lsm.mapper.OlderMapper;
import com.gduf.lsm.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Author lsm
 * @Data
 * 文化活动Service层接口
 */
@Service
public class CulturalService {
    @Autowired
    private CulturalMapper culturalMapper;
    @Autowired
    private JoinCulturalMapper joinCulturalMapper;

    /**
     * 新增
     * @param cultural
     */
    public void add(Cultural cultural) {
        //根据名字查询活动名是否重复
        if(cultural.getCulName()==null){
            throw new CustomException(ResultCodeEnum.CULTURAL_NULL_ERROR);
        }
        Cultural dbCultural = culturalMapper.selectByCulName(cultural.getCulName());
        if (ObjectUtil.isNotNull(dbCultural)) {
            throw new CustomException(ResultCodeEnum.CULTURAL_EXIST_ERROR);
        }
        culturalMapper.insert(cultural);
    }

    /**
     * 删除单个
     * @param id
     */
    public void deleteById(Integer id) {
        culturalMapper.deleteById(id);
    }

    /**
     * 批量删除
     * @param ids
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            culturalMapper.deleteById(id);
        }
    }

    /**
     * 修改
     * @param cultural
     */
    public void updateById(Cultural cultural) {
        culturalMapper.updateById(cultural);
    }

    /**
     * 根据ID查询
     * @param id
     * @return
     */
    public Cultural selectById(Integer id) {
        //先找到此活动
        Cultural cultural=culturalMapper.selectById(id);
        //先判断是否开始，再判断老人是否报名，再判断是否结束
        Date date=new Date();
        System.out.println("*****date*****"+date);
        String start=cultural.getCulStart();
        //开始日期在当前日期之后，未开始
        if(DateUtil.parseDate(start).isAfter(date)){
            cultural.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_NOT_START);
        }else{
            //判断是否报名，由用户是否有存在此活动知是否有老人报名，注意再次点击报名时就要判断老人是否重复报名
            Integer userId= TokenUtils.getCurrentUser().getId();
            List<JoinCultural> joinCultural=joinCulturalMapper.selectByCulIdAndUserId(id,userId);
            if(ObjectUtil.isNotEmpty(joinCultural)){ //注意这里要判断非空才可以
                cultural.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_SIGNED);
            }else{
                cultural.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_NOT_SIGNED);
            }
        }
        //在上面都判断结束后再判断是否结束
        String end=cultural.getCulEnd();
        //结束日期在当前日期之前，已结束
        if(DateUtil.parseDate(end).isBefore(date)){
            cultural.setStatus(ActivityStatusConstant.ACTIVITY_STATUS_OVER);
        }
        return cultural;
    }

    /**
     * 查询所有
     * @param cultural
     * @return
     */
    public List<Cultural> selectAll(Cultural cultural) {
        return culturalMapper.selectAll(cultural);
    }

    /**
     * 分页查询
     * @param cultural
     * @param pageNum
     * @param pageSize
     * @return
     */
    public PageInfo<Cultural> selectPage(Cultural cultural, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Cultural> list = culturalMapper.selectAll(cultural);
        return PageInfo.of(list);
    }
}
